from DataAccess.TickDataProcess import TickDataProcess
from DataAccess.StockIPOInfoProcess import *
from DataAccess.IndexComponentDataProcess import IndexComponentDataProcess
from Utility.InfluxdbUtility import InfluxdbUtility
import influxdb
import pymssql
import dateutil.parser as dtparser
import datetime
#202103-202212,交易日
#code='600000.SH'
#datestart=20211221
#dateend=20211231

#t = TickDataProcess()
#date = t.recordResampleTickShotDataToInfluxdbFromSqlServer(code,datestart,dateend)
#data = t.getTickShotDataFromSql(code,datestart)
#lotsdata = t.updateLotsDataByDate([code],datestart,dateend)
#t.parallelizationUpdateDataByDate([code],datestart,dateend)
#print(data.shape)

database='stocktickdata'
code_index='000300.SH'
datestart=20211231
dateend=20220119

i=IndexComponentDataProcess()
data=i.getDataByDateFromSource(code_index,datestart,dateend)
codeList = list(data['code'].unique())
dateList = list(data['date'].unique())

t = TickDataProcess()
client = influxdb.DataFrameClient(host='localhost', port=8086, username='root', password='', database='')

for code in [codeList[0]]:
    data =[]
    #将从dateList[0]到dateList[-1]的code数据从sql提取出来
    data = t.getTickShotDataMultidayFromSql(code,dateList[0],dateList[-1])   
    #将dateList中每一天的code数据从sql中提取出来(慢)
    #data = t.getTickShotDataListdayFromSql(code,dateList)  
    InfluxdbUtility.saveDataFrameDataToInfluxdbWithClient(client,data,database,code,{})

b = dtparser.parse(str(dateList[0]))+ datetime.timedelta(hours=0)
e = dtparser.parse(str(dateList[-1])) + datetime.timedelta(hours=24)
measure=codeList[0]
query=f""" select * from "{database}"."autogen"."{measure}" where time >= {int(b.timestamp() * 1000 * 1000 * 1000)} and time <= {int(e.timestamp() * 1000 * 1000* 1000)} """
result=client.query(query)
mydata_save = pd.DataFrame(result[measure])

